package com.toolbox.utils

import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

import java.sql.Timestamp
import java.text.SimpleDateFormat

/**
 * Created with IntelliJ IDEA.
 * com.toolbox.utils
 * User: 张斌
 */
@CompileStatic
@TypeChecked
class DateUtil {

    /**
     * 格式化 时间
     * @param time
     * @param fmt ,fmt为空的时候，默认是yyyy-MM-dd HH:mm:ss
     * @return
     */
    static String formatTime(Timestamp time, String fmt) {
        if (time) {
            if (fmt == null) fmt = "yyyy-MM-dd HH:mm:ss"
            SimpleDateFormat myFormat = new SimpleDateFormat(fmt)
            return myFormat.format(time)
        } else {
            return ""
        }
    }

    /**
     * 格式化 时间
     * @param time
     * @param strDateTime
     * @return
     */
    static Long formatDateTime(String strDateTime) {
        if (strDateTime == null || "".equals(strDateTime)) {
            return null
        } else {
            SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            Date myDate = myFormat.parse(strDateTime.trim())
            return myDate.time
        }
    }

    static String formatDateTime2(String strDateTime) {
        if (strDateTime == null || "".equals(strDateTime)) {
            return null
        } else {
            SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd")
            SimpleDateFormat myFormat2 = new SimpleDateFormat("yyyy年MM月dd日")
            Date myDate = myFormat.parse(strDateTime.trim())
            return myFormat2.format(myDate)
        }
    }
    /**
     * 将string类型转换成Long类型
     * @param strDateTime
     * @return
     */
    static Long formatDateString(String strDateTime) {
        if (strDateTime == null || "".equals(strDateTime)) {
            return null
        } else {
            SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            Date myDate = myFormat.parse(strDateTime.trim())
            return myDate.time
        }
    }
    static Long formatDateString(String strDateTime,String fmt) {
        if (strDateTime == null || "".equals(strDateTime)) {
            return null
        } else {
            SimpleDateFormat myFormat = new SimpleDateFormat(fmt)
            Date myDate = myFormat.parse(strDateTime.trim())
            return myDate.time
        }
    }
    /**
     * 格式化 时间
     * @param time
     * @param strDateTime
     * @return
     */
    static Long formatDate(String strDateTime) {
        if (strDateTime == null || "".equals(strDateTime)) {
            return null
        } else {
            SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd")
            Date myDate = myFormat.parse(strDateTime.trim())
            return myDate.time
        }
    }

/**
 * 格式化毫秒数 时间
 * @param time
 * @param fmt ,fmt为空的时候，默认是yyyy-MM-dd HH:mm:ss
 * @return
 */
    static String formatTime(Long time, String fmt) {
        formatTime(getTime(time), fmt)
    }

/**
 * 获取当前 时间
 * @return
 */
    static Timestamp getTime() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        Calendar calendar = Calendar.getInstance()
        String mystrdate = myFormat.format(calendar.getTime())
        return Timestamp.valueOf(mystrdate)
    }

    static String getTimeToString() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        Calendar calendar = Calendar.getInstance()
        String mystrdate = myFormat.format(calendar.getTime())
        return mystrdate
    }

    static Timestamp getTimefmt() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyyMMddHH:mm:ss")
        Calendar calendar = Calendar.getInstance()
        String mystrdate = myFormat.format(calendar.getTime())
        return Timestamp.valueOf(mystrdate)
    }
    static String getTimefmtToString() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyyMMddHH:mm:ss")
        Calendar calendar = Calendar.getInstance()
        String mystrdate = myFormat.format(calendar.getTime())
        return mystrdate
    }
    static Timestamp getTimeString() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyyMMddHHmmss")
        Calendar calendar = Calendar.getInstance()
        String mystrdate = myFormat.format(calendar.getTime())
        return Timestamp.valueOf(mystrdate)
    }
/**
 * 获取今天开始时间
 * yyyy-MM-dd 00:00:00
 * @return
 */
    static Timestamp getDateFirst() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00")
        Calendar calendar = Calendar.getInstance()
        String mystrdate = myFormat.format(calendar.getTime())
        return Timestamp.valueOf(mystrdate)
    }

    /**
     * 获取昨天的日期
     * @return
     */
    static String getDateYesterday() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyyMMdd")
        Calendar calendar = Calendar.getInstance()
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
        String mystrdate = myFormat.format(calendar.getTime())
        return mystrdate
    }

/**
 * 获取今天最后一秒时间
 * yyyy-MM-dd 23:59:59
 * @return
 */
    static Timestamp getDateLast() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59")
        Calendar calendar = Calendar.getInstance()
        String mystrdate = myFormat.format(calendar.getTime())
        return Timestamp.valueOf(mystrdate)
    }

/**
 * 获取今天 日期
 * @return
 */
    static Date getDate() {
        Calendar calendar = Calendar.getInstance()
        return calendar.getTime()
    }

/**
 * 获取 今天的月份
 * 返回格式 yyyyMM
 * @return
 */
    static String getMonth() {
        formatDate(getTime(), "yyyyMM")
    }

/**
 * 根据毫秒数 获取当前时间
 * @param time
 * @return
 */
    static Timestamp getTime(Long time) {
        return new Timestamp(time)
    }

/**
 * 根据毫秒数 格式化成 时间格式
 * @param time
 * @param fmt
 * @return
 */
    static String formatDate(Long time, String fmt) {
        return formatDate(getTime(time), fmt);
    }

/**
 * 格式化日期
 * @param date
 * @param fmt ,fmt为空，默认是 yyyy-MM-dd
 * @return
 */
    static String formatDate(Date date, String fmt) {
        if (date == null) {
            return ""
        }
        if (fmt == null) fmt = "yyyy-MM-dd"
        SimpleDateFormat myFormat = new SimpleDateFormat(fmt)
        return myFormat.format(date)
    }

/**
 * 和当前时间比较，简洁显示
 * 1小时之前，显示到分钟；24小时之前显示到小时；1个月前显示到天
 * @param time
 * @return
 */
    static String simpleRangeTime(Long time) {
        Long cTime = getTime().time - time
        if (cTime < 60000l) return "刚刚"
        if (cTime < 3600000l) return (cTime / (1000 * 60) as int) + "分钟前"
        if (cTime < 86400000l) return (cTime / (1000 * 60 * 60) as int) + "小时前"
        if (cTime < 2592000000l) return (cTime / (1000 * 60 * 60 * 24) as int) + "天前"
        else return "很久以前"
    }

/**
 * 判断是否是48小时之前
 * @param time
 * @return
 */
    static Boolean beforeTime48h(Long time) {
        Long cTime = getTime().time - time
        cTime < 172800000l
    }

    /**
     * 转换成时间 字符串格式必须为 yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd
     * @return
     * @throws java.text.ParseException
     */
    public static Date parseToDate(String val){
        Date date = null;
        if (val != null && val.trim().length() != 0 && !val.trim().toLowerCase().equals("null")){
            val = val.trim();
            if (val.length() > 10) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = sdf.parse(val);
            }
            if (val.length() <= 10) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                date = sdf.parse(val);
            }
        }
        return date;
    }


    /**
     * 返回日期或者时间，如果传入的是日期，返回日期的 00:00:00 时间
     * @param timeString
     * @return
     * @throws Exception
     */
    public static Timestamp getDateFirst(String timeString) throws Exception {
        if (timeString == null || timeString.equals("")) {
            return null;
        }
        if (timeString.length() > 10) {
            return getTime(timeString, "yyyy-MM-dd HH:mm:ss");
        } else {
            return getTime(timeString, "yyyy-MM-dd");
        }
    }

    /**
     * 返回日期或者时间，如果传入的是日期，返回日期的 23:59:59 时间
     * @author 陈旭
     * @param timeString
     * @return
     * @throws Exception
     */
    public static Timestamp getDateLast(String timeString) throws Exception{
        if (timeString == null || timeString.equals("")) {
            return null;
        }
        if (timeString.length() > 10) {
            return getTime(timeString, "yyyy-MM-dd HH:mm:ss");
        } else {
            return getTime(timeString +" 23:59:59", "yyyy-MM-dd HH:mm:ss");
        }
    }

    /**
     * 自定义格式的字符串转换成日期
     * @param timeString
     * @param fmt
     * @return
     * @throws Exception
     */
    public static Timestamp getTime(String timeString, String fmt) throws Exception {
        SimpleDateFormat myFormat = new SimpleDateFormat(fmt);
        Date date = myFormat.parse(timeString);
        myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return Timestamp.valueOf(myFormat.format(date))
    }
    /**
     * 获取系统当前时间
     *
     * @author lijm
     * @return
     */
    public static String getDateTime() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return myFormat.format(new Date());
    }

    /**
     * 获取日期date 一天的起始时间
     * @param date
     * @return
     */
    public static Long dateFirst(Date date){
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00")
        String mystrdate = myFormat.format(date)
        return Timestamp.valueOf(mystrdate).time
    }

    /**
     * 获取日期date 一天的结束时间
     * @param date
     * @return
     */
    public static Long dateLast(Date date){
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59")
        String mystrdate = myFormat.format(date)
        return Timestamp.valueOf(mystrdate).time
    }
    public static int daysBetweenCeil(long startTime,long endTime){
        return Math.ceil((endTime-startTime)/(1000*60*60*24));
    }

}
